package com.sgb.kjwl.view.ui.fragment.machine;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMachineCompanyBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.MachineInEntity;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.adapter.machine.MachineInAdapter;
import com.sgb.kjwl.view.widget.MachineTypeWindow;
import com.sgb.kjwl.view.widget.TwoAreaPopWindow;
import com.sgb.kjwl.viewmodel.machine.MachineCompanyFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author zhengkewen
 */
@Deprecated
public class MachineCompanyFragment extends AppFragment {
    public static final String TAG = "";
    private FragmentMachineCompanyBinding binding;
    private boolean isChange;
    private MainActivity activity;
    private boolean isCreate = false;
    private TwoAreaPopWindow areaPopWindow;
    private MachineTypeWindow machineTypeWindow;
    private List<ParentAreaEntity<List<ParentAreaEntity>>> areaEntityList;
    private List<PublicTypeEntity> publicTypeEntityList;
    private MachineInAdapter adapter;
    @Inject
    MachineCompanyFragModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {

        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_machine_company, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        init();
    }

    private void initView() {
        binding.areaLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        areaPopWindow = new TwoAreaPopWindow(getActivity());
        areaPopWindow.setBgView(binding.grayLayout);
        machineTypeWindow = new MachineTypeWindow(getActivity());
        machineTypeWindow.setBgView(binding.grayLayout);
        if (adapter == null) {
            adapter = new MachineInAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.rlvList.setLayoutManager(layoutManager);
        binding.rlvList.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp1), getActivity().getResources().getColor(R.color.color_line)));
        binding.rlvList.setAdapter(adapter);
        binding.rlvList.setPullRefreshEnabled(false);
        binding.rlvList.setLoadingMoreEnabled(false);
    }

    private void init() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.area_layout:
                if (areaEntityList != null) {
                    showAreaPop();
                } else {
                    viewModel.getMachineAreaModel();
                }
                break;
            case R.id.type_layout:
                if (publicTypeEntityList != null) {
                    showTypePop();
                } else {
                    viewModel.getMachineTypeModel();
                }
                break;
        }
    }

    public void showAreaPop(List<ParentAreaEntity<List<ParentAreaEntity>>> datas) {
        this.areaEntityList = datas;
        showAreaPop();
    }

    private void showAreaPop() {
        machineTypeWindow.dismiss();
        areaPopWindow.showPopwindow(binding.areaLayout);
        areaPopWindow.setData(areaEntityList,isChange);
        isChange = false;
    }

    public void showTypePop(List<PublicTypeEntity> datas) {
        this.publicTypeEntityList = datas;
        showTypePop();
    }

    public void showTypePop() {
        areaPopWindow.dismiss();
        machineTypeWindow.showPopwindow(binding.typeLayout);
        machineTypeWindow.setData(publicTypeEntityList);
    }

    public void showInView(List<MachineInEntity> datas) {
        adapter.setDatas(datas);
    }
}
